package com.xingkeduo.business.operation.controller;

import com.alibaba.fastjson.JSON;
import com.xingkeduo.business.operation.domain.ShopsServprodDomain;
import com.xingkeduo.pojo.json.ServprodJson;
import com.xingkeduo.pojo.json.ShopsServprodJson;
import com.xingkeduo.pojo.query.ShopsServprodQuery;
import com.xingkeduo.pojo.table.ServprodTable;
import com.xingkeduo.pojo.table.ShopsServprodTable;
import com.xingkeduo.util.pojo.base.BaseController;
import com.xingkeduo.util.tool.ReturnUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

import static com.xingkeduo.tool.OperaConstants.CMS_URL_PREFIX;

/**
 * 门店服务商品管理
 *
 * @author wangxilong <wangxilong@xingkeduo.com>
 * @version id:v0.1 ${date}${time}
 * @package BeautyOS
 * @copyright Copyright (c) XKD 2019
 */
@RestController
@RequestMapping(CMS_URL_PREFIX +"/shopsServprod")
public class ShopsServprodController extends InitController {

    @Autowired
    private ShopsServprodDomain shopsServprodDomain;

    //查看
    @RequestMapping(value = "/{urlParam:.+}", method = RequestMethod.GET)
    public String doGet(@PathVariable String urlParam) {
        returnUtil = new ReturnUtil();
        try {
            ShopsServprodQuery shopsServprodQuery = ShopsServprodJson.createQueryForShopsServprod(urlParam);
            long code = shopsServprodQuery.getCode();
            ShopsServprodTable shopsServprodTable = shopsServprodDomain.getShopsServprodByCode(code);
            return returnUtil.getSuccess(ShopsServprodJson.toJson(shopsServprodTable));
        } catch (Throwable throwable) {
            return dealException(throwable, returnUtil);
        }
    }

    //查看
    @RequestMapping(value = "/lists/{urlParam:.+}", method = RequestMethod.GET)
    public String doGetLists(@PathVariable String urlParam) {
        returnUtil = new ReturnUtil();
        try {
            ShopsServprodQuery shopsServprodQuery =  JSON.parseObject(urlParam,ShopsServprodQuery.class);

            List<ShopsServprodTable> shopsServprodTableList = shopsServprodDomain.getShopsServprodByShopsId(shopsServprodQuery);

            return returnUtil.getSuccess(ShopsServprodJson.toJson(shopsServprodTableList));

        } catch (Throwable throwable) {
            return dealException(throwable, returnUtil);
        }
    }


    //添加
    @RequestMapping(value = "", method = RequestMethod.POST)
    public String doPost(@RequestBody @Valid ShopsServprodTable shopsServprodVo , ServerHttpRequest request) {
        returnUtil = new ReturnUtil();
        try {
            //this.errMsg(bindingResult);
            shopsServprodVo.setCreatedBy(this.getOperator(request));
            shopsServprodDomain.createShopsServprod(shopsServprodVo);
            return returnUtil.getSuccess();
        } catch (Throwable throwable) {
            return dealException(throwable, returnUtil);
        }
    }

    //修改
    @RequestMapping(value = "/{urlParam}", method = RequestMethod.PUT)
    public String doPut(@PathVariable String urlParam, @RequestBody List<ShopsServprodTable> shopsServprodList ,ServerHttpRequest request) {
        returnUtil = new ReturnUtil();
        try {
            ShopsServprodQuery shopsServprodQuery =  JSON.parseObject(urlParam,ShopsServprodQuery.class);
           // shopsServprodQuery.setUpdatedBy(this.getOperator(request));
            List<ShopsServprodTable> ShopsServprodTable = new ArrayList<>();
            if(shopsServprodList!=null){
                for (ShopsServprodTable shopsServprodTable : shopsServprodList) {
                    shopsServprodTable.setUpdatedBy(this.getOperator(request));
                    ShopsServprodTable.add(shopsServprodTable);
                }

            }
            shopsServprodDomain.modifyShopsServprod(shopsServprodQuery,ShopsServprodTable);
            return returnUtil.getSuccess();
        } catch (Throwable throwable) {
            return dealException(throwable, returnUtil);
        }
    }

    //删除
    @RequestMapping(value = "/{urlParam}", method = RequestMethod.DELETE)
    public String doDelete(@PathVariable String urlParam) {
        returnUtil = new ReturnUtil();
        try {
            ShopsServprodQuery shopsServprodQuery = ShopsServprodJson.createQueryForShopsServprod(urlParam);
            long code = shopsServprodQuery.getCode();
            shopsServprodDomain.delShopsServprod(code);
            return returnUtil.getSuccess();
        } catch (Throwable throwable) {
            return dealException(throwable, returnUtil);
        }
    }
}
